import os
from frames import Frame
from global_names import *

class Video():
	"""
	The Video class defines a video as a list of Frames.
	
	"""
	def __init__(self,video_dir,ignore_framerange = False):
		video_path = video_dir + '/video/'
		self.params_path = video_dir + '/data/data.json'
		self.params = self.load_params()
		if ignore_framerange:
			framerange = [None,None]
		else:
			framerange = self.params['frame_range']
		self.frames = self.load_frames(video_path, framerange)
		
		self.name = video_dir
		self.shortname = self.name[self.name.rindex('/'):]
	def load_params(self):
		try:
			data = json.loads(open(self.params_path, 'r').read())
		except IOError:
			print "Video class unable to load video parameters specified by path."
			exit()
		return data
	def store_params(self, new_params):
		data = json.loads(open(self.params_path, 'r').read())
		data['brightness'] = new_params['brightness']
		data['contrast'] = new_params['contrast']
		data['canny_thresh1'] = new_params['canny_thresh1']
		data['canny_thresh2'] = new_params['canny_thresh2']
		data['kern_size'] = new_params['kern_size']
		f = open(self.params_path, 'w')
		f.write("%s"%json.dumps(data))
		f.close()
	def load_frames(self, video_path, frame_range):
		frames = []
		for frame_name in os.listdir(video_path)[frame_range[0]:frame_range[1]]:
			frames.append(Frame(video_path+frame_name, self.params))
		return frames
	def empty_frame(self):
		return np.zeros(np.shape(self.frames[0].image))
		